// const { EEventType } = require("XrFrame");

// pages/try/upload/upload.js
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgs: [],
    socketOpen: false,
    src: '',
    canvasStyle: {},
    xiangxi: '',

    jindu: '',
    dizhi: '',

    weidu: '',

    opacity: false, // 显示图标
    opacityimg: true, //隐藏封面

    SHANGHU_CODE: '',

    cunkuan: '0',
    baoxian:'0',
    licai: '0',
    jijing: '0',

    zhiliang: true,

    zhihui: false,
    flag: true,

    shijian: '',
    VISITING_IMG: '无', //带定位的图片
    VISITING_DETAILS: '', //走访详情
    SHANGHU_NAME: '', //商家名称

    VISITING_TIME: '', //走访时间、
    VISITING_NAME:'',
    ENTERPRISE_CONTACT_PERSON:'',
    BASIC_INFORMATION_OF_THE_ENTERPRISE:'',
  },








  handleInput(e) {
    let value = this.validateNumber(e.detail.value)
    this.setData({
      cunkuan: value
    })

    console.log(this.data.cunkuan);
  },


  handleInputa(e) {
    let value = this.validateNumber(e.detail.value)
    this.setData({
      baoxian: value
    })
    console.log(this.data.baoxian);
  },



  handleInputb(e) {
    let value = this.validateNumber(e.detail.value)
    this.setData({
      licai: value
    })
    console.log(this.data.licai);
  },



  handleInputc(e) {
    let value = this.validateNumber(e.detail.value)
    this.setData({
      jijing: value
    })
    console.log(this.data.jijing);
  },



  validateNumber(val) {
    return val.replace(/\D/g, '')
  },






  back() {
    wx.navigateBack({
      delta: 1,
    })
  },


  reset() {
    this.setData({
      cunkuan: '0',
      baoxian:'0',
      licai: '0',
      jijing: '0',
      zhiliang: true,
      zhihui: false,
      VISITING_IMG: '无', //带定位的图片
      VISITING_NAME: '', //业务员名称
      SHANGHU_NAME: '', //商家名称
      VISITING_DETAILS: '', //走访详情
      VISITING_TIME: '', //走访时间
      // avatar: '', //图片地址
      imgs: [],
      opacity: false, // 显示图标
      opacityimg: true, //隐藏封面
    })
  },



  Photograph() { //点击拍照的方法

    let that = this
    wx.chooseImage({
      count: 1, //允许的上传总数
      quality: 'high', //图片质量
      sizeType: ['compressed'],
      sourceType: ['camera'], //支持相机和相册
      success: function (res) {
        console.log(res.tempFilePaths[0])
        wx.showLoading({
          title: "正在加载图片",
          mask: true
        })
        //获取原图片信息
        wx.getImageInfo({
          src: res.tempFilePaths[0],
          success: function (res) {
            console.log(res)
            var width = res.width,
              height = res.height;
            that.setData({ //设定画布的宽高
              w: width,
              h: height
            })
            //获取当前时间
            let newDate = new Date();
            console.log(newDate);
            let year = newDate.getFullYear() //年
            let month = newDate.getMonth() + 1 //月
            let day = newDate.getDate() //日
            var hour = newDate.getHours()
            // var minute = newDate.getMinutes()


            var minute = newDate.getMinutes().toString().padStart(2, '0')
            // console.log(minute);

            var second = newDate.getSeconds() < 10 ? "0" + newDate.getSeconds() : newDate.getSeconds();
            let roleNameInfo = '拍摄时间：' + year + '年' + month + '月' + day + '日 ' + hour + ':' + minute + ':' + second
            let roleNameInfoaaa = year + '年' + month + '月' + day + '日 ' + hour + ':' + minute + ':' + second
            // let address = "that.data.dizhi" + "that.data.dizhi"//动态地址

            console.log(roleNameInfo);

            let address = that.data.xiangxi
            let jinweidu = `经度:${that.data.jindu}纬度:${that.data.weidu}`
            console.log(address);
            //创建canvas
            const ctx = wx.createCanvasContext('firstCanvas');
            ctx.drawImage(res.path, 0, 0, width, height); //先画出图片
            //将声明的时间放入canvas
            ctx.setFontSize(40) //注意：设置文字大小必须放在填充文字之前，否则不生效
            ctx.setFillStyle('red');

            ctx.fillText(roleNameInfo, 80, height - 800);
            ctx.fillText(address, 80, height - 750);
            ctx.fillText(jinweidu, 80, height - 700);

            ctx.draw(false, function () {
              setTimeout(function () {
                //绘画完成回调
                //生成图片
                wx.canvasToTempFilePath({
                  quality: 1,
                  fileType: 'jpg',
                  canvasId: 'firstCanvas',
                  success: function (ress) {
                    wx.hideLoading();
                    let imgs = that.data.imgs
                    imgs.push(ress.tempFilePath)
                    that.setData({
                      imgs: imgs
                    })
                    console.log('imgs', that.data.imgs)
                    console.log(ress.tempFilePath); //ress.tempFilePath就是带有水印的图片路径


                    wx.uploadFile({
                      url: 'https://jiagaojie.xajiarong.net/youchuhuiqin/Test/API_1169_uploadFileCOS', // 
                      method: 'POST',
                      name: "FIMG",
                      filePath: ress.tempFilePath,
                      header: {
                        'Content-Type': 'multipart/form-data'
                      },
                      formData: {
                        KEY: 'xianjgjKey9999',
                        OPEN_ID: that.data.openid, //用户OPEN_ID
                        session_key: that.data.session_key,
                        OPEN_UUID: that.data.jiami
                      },
                      success: function (res) {
                        console.log(JSON.parse(res.data));
                        let imgpath = JSON.parse(res.data)
                        console.log('hahah', imgpath);
                        if (imgpath.result == "success" && imgpath.newFile) {
                          wx.showToast({
                            title: '上传成功',
                            icon: 'success',
                            duration: 1500,
                          })
                          console.log('44444444', imgpath.newFile);

                          setTimeout(() => {
                            that.setData({
                              VISITING_IMG: imgpath.newFile,
                              VISITING_TIME: roleNameInfoaaa,
                              // opacity: true, // 隐藏icon图标
                              // opacityimg: false, //显示封面
                              zhiliang: false,
                              zhihui: true,
                            })
                          }, 500)
                          console.log('上传后返回的地址', that.data.videpath);
                        } else {
                          wx.showModal({
                            title: '上传视频失败',
                            content: '请重新上传',
                            showCancel: false,
                          });
                        }
                      }
                    })

                  }
                })
                that.setData({
                  opacity: true, // 隐藏icon图标
                  opacityimg: false, //显示封面
                })
              }, 600)
            })
          }
        })
      }
    })
  },



  // getWhere() {


  // },


  // 删除图片
  deleteImg: function (e) {
    var imgs = this.data.imgs;
    var index = e.currentTarget.dataset.index;
    imgs.splice(index, 1);
    this.setData({
      imgs: imgs
    });
  },
  // 预览图片
  previewImg: function (e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    var imgs = this.data.imgs;
    console.log(imgs[index].tempFilePath)
    wx.previewImage({
      //当前显示图片
      current: imgs[index],
      //所有图片
      urls: imgs
    })
  },


  //   VISITING_NAME,
  //  VISITING_TIME,
  //   SHANGHU_NAME,
  //  VISITING_DETAILS,
  //  VISITING_IMG,
  //  OPEN_ID,
  //  OPEN_UUID,
  //   session_key,
  // EMPLOYEE_ID

  submit() {

    var that = this

    // that.huoquxingdingwei()

    console.log('111', that.data.jindu);

    console.log('555', that.data.VISITING_NAME);
    console.log('555', that.data.SHANGHU_NAME);
    console.log('555', that.data.VISITING_DETAILS);
    console.log('555', that.data.VISITING_IMG);
    console.log('555', that.data.VISITING_TIME);

  //  if ( that.data.jindu && that.data.VISITING_NAME && that.data.SHANGHU_NAME && that.data.VISITING_DETAILS && that.data.VISITING_IMG && that.data.VISITING_TIME) {
    if (that.data.SHANGHU_NAME == '') {

      wx.showModal({
        content: '请填写企业名称',
        showCancel: false,
      });



    } else
    
    
    if (that.data.ENTERPRISE_CONTACT_PERSON == '') {
      console.log(111111);

      wx.showModal({
        content: '请填写企业联系人',
        showCancel: false,
      });


    } else 
    if (that.data.VISITING_DETAILS == '') {
      console.log(111111);

      wx.showModal({
        content: '请填写需求意向',
        showCancel: false,
      });


    } else 
  
    if (that.data.BASIC_INFORMATION_OF_THE_ENTERPRISE == '') {
      console.log(111111);

      wx.showModal({
        content: '请填写企业基本情况',
        showCancel: false,
      });


    } else

if (that.data.cunkuan == ''|| that.data.baoxian == ''||that.data.licai == ''||that.data.jijing == '') {

      wx.showModal({
        content: '请查看业务需求是否填写完整',
        showCancel: false,
      });


      console.log('555', that.data.cunkuan);
      console.log('555', that.data.baoxian);
      console.log('555', that.data.licai);
      console.log('555', that.data.jijing);






      console.log('555', that.data.DANCE_NAME);



    } else   if (that.data.jindu == '') {

      wx.showModal({
        content: '请获取经纬度',
        showCancel: false,
      });



    } else if (that.data.VISITING_TIME == '') {

      wx.showModal({
        content: '请获取时间',
        showCancel: false,
      });



    } else if (that.data.VISITING_NAME == '') {

      wx.showModal({
        content: '请填写走访业务员名称',
        showCancel: false,
      });

    } else {
      wx.showLoading({
        title: '加载中...',
      })

      wx.request({
        url: app.globalData.interface+'/squareDance/API_5206_shanghuVisiting',
        method: 'POST',
        data: {

          VISITING_NAME: that.data.VISITING_NAME,
          VISITING_TIME: that.data.VISITING_TIME,
          SHANGHU_NAME: that.data.SHANGHU_NAME,
          VISITING_DETAILS: that.data.VISITING_DETAILS,
          VISITING_IMG: that.data.VISITING_IMG,
          EMPLOYEE_ID: that.data.mealsList,
          OPEN_ID: that.data.openid, //用户OPEN_ID
          session_key: that.data.session_key,
          OPEN_UUID: that.data.jiami,
          LONGITUDE: that.data.jindu,
          DIMENSION: that.data.weidu,
          SHANGHU_CODE: that.data.SHANGHU_CODE,
          SHANGHU_ADD: that.data.xiangxi,
          ENTERPRISE_CONTACT_PERSON: that.data.ENTERPRISE_CONTACT_PERSON,
          BASIC_INFORMATION_OF_THE_ENTERPRISE: that.data.BASIC_INFORMATION_OF_THE_ENTERPRISE,
          // VISITING_BUSINESS:`存款${that.data.cunkuan}万，保险${that.data.baoxian}万，理财${that.data.licai}万，基金${that.data.jijing}万` 
          // VISITING_BUSINESS: `${that.data.cunkuan}万`,
          VISITING_BUSINESS: that.data.cunkuan,
          VISITING_INSURANCE: that.data.baoxian,
          VISITING_WEALTH: that.data.licai,
          VISITING_FUND: that.data.jijing,





        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log('res', res);
          console.log('res', res.data.result);

          if (res.data.result == 'success') {
            wx.hideLoading()
            const text = res.data.data
            wx.showModal({
              content: text,
              showCancel: false,
            });

          } else {
            wx.showModal({
              content: res.data.data,
              showCancel: false,
            });
            wx.hideLoading()
          }
        },
        fail: (res => {
          wx.hideLoading()
        })
      })

    }





  },




  huoqushijian() {

    //获取当前时间
    let newDate = new Date();
    console.log(newDate);
    let year = newDate.getFullYear() //年
    let month = newDate.getMonth() + 1 //月
    let day = newDate.getDate() //日
    var hour = newDate.getHours()
    // var minute = newDate.getMinutes()


    var minute = newDate.getMinutes().toString().padStart(2, '0')
    // console.log(minute);

    var second = newDate.getSeconds() < 10 ? "0" + newDate.getSeconds() : newDate.getSeconds();
    let roleNameInfo = year + '年' + month + '月' + day + '日 ' + hour + ':' + minute + ':' + second


    console.log(roleNameInfo);
    this.setData({
      VISITING_TIME: roleNameInfo,

    })

  },

  // huoqudingwwi() {
  huoquxingdingwei() {
    if (this.data.flag) {
      this.data.flag = false
      let that = this
      console.log(that.data.jiami);
      wx.getLocation({
        type: 'wgs84',
        success(res) {
          console.log(res)
          const latitude = res.latitude
          const longitude = res.longitude
          const speed = res.speed
          const accuracy = res.accuracy
          console.log(latitude);
          console.log(longitude);
          console.log(speed);
          console.log(accuracy);

          if (res.latitude != '') {
            wx.request({
              url: app.globalData.interface+'/jiarong/API_8005_getAdd',
              method: 'POST',
              data: {
                longitude: res.longitude,
                latitude: res.latitude,
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: res => {
                that.setData({
                  xiangxi: res.data.data,
                  jindu: longitude,
                  weidu: latitude,
                })
              }
            })
          } else {
            console.log(111);
          }
        }
      })



      if (!this.data.mealsList) {
        wx.navigateTo({
          url: '../../pages/xianyuangongdenglu/xianyuangongdenglu'
        })
      }
      setTimeout(() => {
        this.data.flag = true
      }, 5000)

    }
  },




  onLoad(options) {

    this.setData({
      openid: app.globalData.openid,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,
      mealsList: app.globalData.mealsList,

    })


  },
  onShow() {


    var that = this

    if (that.data.jiami == '' || that.data.session_key == '') {

      wx.login({
        success: function (res) {
          console.log(res);
          if (res.code) { //wx.login获取code。
            console.log(res.code);
            //发起网络请求

            wx.request({
              url: app.globalData.interface+'/post/API_1062_appGetOpenid',
              // url: 'http://192.168.3.173:8081/post/API_1062_appGetOpenid',
              method: 'GET',
              //向后端发送的数据
              data: {
                code: res.code //将code发送到后台服务器。
              },
              header: {
                "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
              },
              success: res => {
                console.log(res);
                if (res.data.result != 'success') {
                  console.log(1);
                  // wx.showToast({
                  //   title: '请获取手机号',
                  //   duration: 1000
                  // })
                } else {
                  var openid = res.data.openid;
                  app.globalData.openid = openid;

                  console.log(res.data.openid);
                  that.setData({
                    openid: res.data.openid
                  })
                  // that.scanCod()

                  that.setData({
                    phone: app.globalData.phone,

                    jiami: app.globalData.jiami,
                    OPEN_ID: app.globalData.openid,
                    openid: app.globalData.openid,
                    session_key: app.globalData.session_key,
                  })
                  console.log(that.data.phone);
                  console.log(that.data.jiami);
                  console.log(that.data.openid);
                  console.log(that.data.session_key);
                }

              }
            })
          } else {
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        }
      });
    } else {

      console.log('成功');
    }



    // this.huoqudingwwi()
    // this.getWhere()





  },
})